查看原文
其他

回溯亚群细分效果到初始分群

柠檬不酸 单细胞天地 2022-08-10

分享是一种态度

  • 1 加载R包

  • 2 读取Seurat object

  • 3 提取子集并做亚群分析

  • 4 修改亚群命名,便于区分初始分群序号

  • 5 回溯亚群细分效果到初始分群


1 加载R包

library(Seurat)
library(ggplot2)

2 读取Seurat object

# Remove variables
rm(list = ls())

# Set workdir
workdir <- 'F:/R_Language/R_Practice/scRNA_Seq_column'
setwd(workdir)

# Load seurat object variable
data.filt <- readRDS(file = "data/Raw_data/Cluster_all_again.Rdata")
print(table(Idents(data.filt)))
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 406 399 352 340 332 283 247 208 184 181 159 81 56 47 41 37
DT::datatable(data.filt@meta.data)

3 提取子集并做亚群分析

sub_cells <- subset(data.filt, cluster == 1)
DT::datatable(sub_cells@meta.data)

# Determine the K-nearest neighbor graph
sub_cells <- FindNeighbors(sub_cells, reduction="pca", dims = 1:20, verbose = F)
# Determine the clusters for various resolutions
sub_cells <- FindClusters(sub_cells, resolution = 0.5, verbose = F)

# t-SNE and Clustering
sub_cells <- RunTSNE(sub_cells, reduction = "pca", dims = 1:20, verbose = F) # check_duplicates = FALSE, Remove duplicates before running TSNE
sub_cells <- RunUMAP(sub_cells, reduction = "pca", dims = 1:20, verbose = F)

4 修改亚群命名,便于区分初始分群序号

## Rename sub cells
metadata <- sub_cells@meta.data
metadata$cluster <- Idents(sub_cells)
metadata$sub_cluster <- paste0('M', metadata$cluster)
sub_cells@meta.data <- metadata
table(sub_cells$sub_cluster)
##
## M0 M1 M2
## 170 151 85

5 回溯亚群细分效果到初始分群

## Mapping to old clusters
metadata <- data.filt@meta.data
metadata$cell_type <- NA
metadata$cell_type <- sub_cells@meta.data[match(rownames(data.filt@meta.data), rownames(sub_cells@meta.data)), 'sub_cluster']
print(table(metadata$cell_type))
##
## M0 M1 M2
## 170 151 85
DT::datatable(metadata)

celltype_names <- NULL
for(i in 1:dim(metadata)[1]){
# print(i)
sub_data <- metadata[i,]
if(is.na(sub_data$cell_type)){
# print('Change value')
sub_data$cell_type <- sub_data$cluster
celltype_names <- c(celltype_names, sub_data$cell_type)
}else{
celltype_names <- c(celltype_names, sub_data$cell_type)
}
}

print(table(celltype_names))
## celltype_names
## 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 M0 M1 M2
## 181 159 81 56 47 41 37 399 352 340 332 283 247 208 184 170 151 85
metadata$cell_type <- celltype_names
metadata$cell_type <- factor(metadata$cell_type)
metadata$cell_type <- factor(metadata$cell_type, levels = c(names(table(sub_cells$sub_cluster)), 2:nlevels(Idents(data.filt))))
print(table(metadata$cell_type))
##
## M0 M1 M2 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 170 151 85 399 352 340 332 283 247 208 184 181 159 81 56 47 41 37
data.filt@meta.data <- metadata

DimPlot(data.filt, reduction='tsne', label=T, label.size=5, pt.size=1, group.by='cluster') + ggtitle('original celltype')

DimPlot(data.filt, reduction='tsne', label=T, label.size=5, pt.size=1, group.by='cell_type') + ggtitle('new celltype')


往期回顾

对细胞簇重命名

OSCA单细胞数据分析笔记10—Marker gene detection

NC单细胞文章复现(七):Gene expression signatures(2)

多组学分析肺结核队列的记忆T细胞状态






如果你对单细胞转录组研究感兴趣,但又不知道如何入门,也许你可以关注一下下面的课程



看完记得顺手点个“在看”哦!


生物 | 单细胞 | 转录组丨资料每天都精彩

长按扫码可关注

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存